package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Detectstandard;
import cn.edu.scau.cmi.ema.domain.Feature;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("DetectstandardController")
public class DetectstandardController extends CmiEmaController {

	@RequestMapping("/listDetectstandards")
	public ModelAndView listDetectstandards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectstandardPaginationService.updatePagination(request, pagination);
		Set<Detectstandard> detectstandards = detectstandardPaginationService.getPaginationDetectstandards(pagination);

		mav.addObject("detectstandards", detectstandards);
		mav.addObject("entityName", "detectstandard");
		mav.addObject("pagination", pagination);
		mav.setViewName("detectstandard/listDetectstandards");
		return mav;
	}

	public String listDetectstandards() {
		return "redirect:/listDetectstandards";
	}

	@RequestMapping("/viewDetectstandard")
	public ModelAndView viewDetectstandard(@RequestParam Integer detectstandardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detectstandard", detectstandardDAO.findDetectstandardByPrimaryKey(detectstandardId));
		mav.setViewName("detectstandard/viewDetectstandard");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetectstandards")
	public ModelAndView confirmDeleteDetectstandards(
			@RequestParam(value = "deleteDetectstandardsIds", required = true) Set<Integer> deleteDetectstandardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detectstandard> deleteDetectstandards = detectstandardDAO
				.findDetectstandardsByIds(deleteDetectstandardsIds);
		Set<Detectstandard> referencedDetectstandards = detectstandardService
				.loadReferenceDetectstandards(deleteDetectstandards);
		boolean hasReferenced;
		if (referencedDetectstandards.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetectstandards);
			hasReferenced = false;
		} else {
			deleteDetectstandards.removeAll(referencedDetectstandards);
			mav.addObject("referencedEntitys", referencedDetectstandards);
			mav.addObject("deleteEntitys", deleteDetectstandards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detectstandard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetectstandards");
		return mav;
	}

	@RequestMapping("/deleteDetectstandard")
	public String deleteDetectstandard(@RequestParam Integer detectstandardId) {
		Detectstandard detectstandard = detectstandardDAO.findDetectstandardByPrimaryKey(detectstandardId);
		detectstandardService.deleteDetectstandard(detectstandard);
		return "forward:/listDetectstandards";
	}

	@RequestMapping("/confirmDeleteDetectstandard")
	public ModelAndView confirmDeleteDetectstandard(@RequestParam Integer detectstandardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detectstandard", detectstandardDAO.findDetectstandardByPrimaryKey(detectstandardId));

		mav.setViewName("detectstandard/deleteDetectstandard");

		return mav;
	}

	@RequestMapping("/deleteDetectstandards")
	public String deleteDetectstandards(
			@RequestParam(value = "deleteDetectstandardsId", required = true) Set<Integer> deleteDetectstandardsId) {
		detectstandardService.deleteDetectstandards(deleteDetectstandardsId);
		return "forward:/listDetectstandards";
	}

	@RequestMapping("/editDetectstandard")
	public ModelAndView editDetectstandard(@RequestParam Integer detectstandardId) {

		ModelAndView mav = new ModelAndView();

		Detectstandard detectstandard = detectstandardDAO.findDetectstandardByPrimaryKey(detectstandardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detectstandard", detectstandard);

		// 把所有的引用detectstandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectstandardService.loadDetectstandardObjectReferencedSet(detectstandard));

		// 将编辑页面detectstandard添加到mav中
		mav.setViewName("detectstandard/editDetectstandard");
		return mav;
	}

	@RequestMapping("/newDetectstandard")
	public ModelAndView newDetectstandard() {
		ModelAndView mav = new ModelAndView();
		Detectstandard detectstandard = new Detectstandard();

		mav.addObject("detectstandard", detectstandard);

		// 把所有的引用detectstandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectstandardService.loadDetectstandardObjectReferencedSet(detectstandard));

		mav.addObject("newFlag", true);
		mav.setViewName("detectstandard/editDetectstandard");

		return mav;
	}

	@RequestMapping("/saveDetectstandard")
	public ModelAndView saveDetectstandard(@ModelAttribute Detectstandard detectstandard) {

		ModelAndView mav = new ModelAndView();

		Feature featureTemp = detectstandard.getFeature();
		Feature feature = featureTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(featureDAO.findFeatureById(featureTemp.getId()));
		Food foodTemp = detectstandard.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));
		Unit unitTemp = detectstandard.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Detectstandard existingDetectstandard = CmiHibernateProxyUtil.deproxy(detectstandardDAO
				.findDetectstandardByPhysicalPrimaryKey(food.getId(), feature.getId(), detectstandard.getDate()));

		detectstandard.setFeature(feature);
		detectstandard.setFood(food);
		detectstandard.setUnit(unit);
		// 新建记录
		if (detectstandard.getId() == null) {
			if (detectstandard.equals(existingDetectstandard)) {

				mav.addAllObjects(detectstandardService.loadDetectstandardClassReferencedSet(detectstandard));
				// mav = editContinue(mav, detectstandard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectstandard/editDetectstandard");
			} else {
				detectstandardService.saveDetectstandard(detectstandard);
				mav.setViewName("forward:/listDetectstandards");
			}
		}
		// 编辑记录
		else {
			Detectstandard originDetectstandard = CmiHibernateProxyUtil
					.deproxy(detectstandardDAO.findDetectstandardByPrimaryKey(detectstandard.getId()));
			if (!detectstandard.equals(originDetectstandard) && detectstandard.equals(existingDetectstandard)) {
				mav.addAllObjects(detectstandardService.loadDetectstandardClassReferencedSet(detectstandard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectstandard/editDetectstandard");
			} else {
				detectstandardService.saveDetectstandard(detectstandard);
				mav.setViewName("forward:/listDetectstandards");
			}
		}
		return mav;
	}

	@RequestMapping("/detectstandardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
